package com.soft.javase.ch08.test.test4;

import java.util.LinkedList;

/**
 * @author wangzhisen
 * @Desc TODO
 * @date 2025/4/29
 */
public class LinkedListTest4 {

    public static void main(String[] args) {
        //queue();
        stack();
    }

    //用LinkedList集合，模拟队列操作   队列：先进先出
    public static void queue(){
        LinkedList<Person> list = new LinkedList();
        //offer 添加元素
        list.offer(new Person("zs" , 18));
        list.offer(new Person("ls" , 22));
        list.offer(new Person("ww" , 19));
        list.offer(new Person("lss" , 26));
        list.offer(new Person("cq" , 19));
        //poll 获取并移除元素
        int size = list.size();
        for (int i = 0; i < size; i++) {
            System.out.println(list.poll());
        }
    }

    //用LinkedList集合，模拟堆栈操作  堆栈：先进后出
    public static void stack(){
        LinkedList<Person> list = new LinkedList();
        //push 添加元素
        list.push(new Person("zs" , 18));
        list.push(new Person("ls" , 22));
        list.push(new Person("ww" , 19));
        list.push(new Person("lss" , 26));
        list.push(new Person("cq" , 19));
        //pop 弹出元素
        int size = list.size();
        for (int i = 0; i < size; i++) {
            System.out.println(list.pop());
        }
    }
}

class Person{
    private String username;
    private int age;

    public Person() {
    }

    public Person(String username, int age) {
        this.username = username;
        this.age = age;
    }

    /**
     * 获取
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * 设置
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Person{username = " + username + ", age = " + age + "}";
    }
}
